package com.future.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.domain.Room;
import com.future.domain.Storey;
import com.future.domain.Student;
import com.future.dto.MsgDto;
import com.future.dto.StoreyDto;
import com.future.dto.StudentForRoomDto;
import com.future.service.RoomService;
import com.future.service.StudentService;
import com.future.service.UserinfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: 田坑坑
 * @Date: 2022/6/28 14:49
 * @Description: 我题序等你回
 */

@RestController
@RequestMapping("student")
public class StudentController {


    @Autowired
    UserinfoService  userinfoService;

    @Autowired
    StudentService   studentService;

    @Autowired
    RoomService      roomService;

    //分页查询
    @PostMapping("selectStudents")
    public PageInfo<StudentForRoomDto> selectstorey(StudentForRoomDto studentForRoomDto){

        PageHelper.startPage(studentForRoomDto.getCurrent(),studentForRoomDto.getSize());
        List list    =   this.userinfoService.studentForRoom();

        PageInfo pageInfo=new PageInfo(list);


        return pageInfo;


    }


    // 根据 学生编号  修改学生寄宿状态
    @PostMapping("updateStatus")
    public MsgDto changeStudent(Student student){

        System.out.println(student);

        // 不寄宿了
        if(student.getStudentstatus().equals("否")){

            //1.修改学生寄宿状态,清除所在楼栋数据
            student.setStoreyid(null);

            //2 .清除所在宿舍 数据后  ——》 将宿舍剩余床位加1

            //这是当前 宿舍id
            int  roomId   =   student.getRoomid();
            //根据 宿舍id 获取 宿舍对象
            Room   room   =  this.roomService.getById(roomId);

            // 该宿舍剩余床位加 1
             room.setRoomresidue(room.getRoomresidue()+1);

            //修改当前 宿舍 的剩余床位
            this.roomService.updateById(room);


            student.setRoomid(null);


            this.studentService.updateById(student);
            return   new MsgDto(true,"修改成功",student);

        }



            return   new MsgDto(true,"修改成功");




    }
}
